package com.dxy.dxyproduct.controller;

import com.dxy.dxycommon.VO.ResultVO;
import com.dxy.dxycommon.utils.ResultVOUtil;
import com.dxy.dxyproduct.DO.ProductInfo;
import com.dxy.dxyproduct.VO.ProductCategoryTree;
import com.dxy.dxyproduct.VO.ProductCategoryVO;
import com.dxy.dxyproduct.VO.ProductInfoVO;
import com.dxy.dxyproduct.enums.ResultEnum;
import com.dxy.dxyproduct.exception.ProductException;
import com.dxy.dxyproduct.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

//产品控制器
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 添加分类
     *
     * @param productCategoryVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/addCategory", produces = "application/json;charset=UTF-8")
    public ResultVO addCategory(@RequestBody @Validated ProductCategoryVO productCategoryVo, BindingResult bindingResult) {
        String categoryType;
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(Objects.requireNonNull(bindingResult.getFieldError().getDefaultMessage()));
        } else {
            categoryType = productService.addCategory(productCategoryVo);
        }
        return ResultVOUtil.success(categoryType);
    }

    /**
     * 分类删除
     *
     * @param categoryType
     * @return
     */
    @RequestMapping(value = "/deleteCategory")
    public ResultVO deleteCategory(BigInteger categoryType) {
        int backNumber = productService.deleteCategory(categoryType);
        if (backNumber == 0) {
            throw new ProductException(ResultEnum.CATEGORY_DELETE_FAIL);
        }
        return ResultVOUtil.success();
    }

    /**
     * 更新分类
     *
     * @param productCategoryVo
     * @param bindingResult
     * @return
     */
    @RequestMapping(value = "/updateCategory", produces = "application/json;charset=UTF-8")
    public ResultVO updateCategory(@RequestBody ProductCategoryVO productCategoryVo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(Objects.requireNonNull(bindingResult.getFieldError().getDefaultMessage()));
        } else {
            productService.updateCategory(productCategoryVo);
        }
        return ResultVOUtil.success();
    }

    /**
     * 查询分类
     *
     * @return
     */
    @RequestMapping(value = "/listCategory")
    public ResultVO listCategory() {
        List<ProductCategoryTree> productCategoryTree = productService.listCategory();
        return ResultVOUtil.success(productCategoryTree);
    }

    /**
     * 新增商品
     *
     * @param productInfoVO
     * @return
     */
    @RequestMapping(value = "/addProduct", produces = "application/json;charset=UTF-8")
    public ResultVO addProduct(@RequestBody @Validated ProductInfoVO productInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        String productId = productService.addProduct(productInfoVO);
        return ResultVOUtil.success(productId);
    }


    /**
     * 删除商品(下架)
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteProduct", produces = "application/json;charset=UTF-8")
    public ResultVO deleteProduct(String id) {
        productService.deleteProduct(id);
        return ResultVOUtil.success(id);
    }


    /**
     * 更新商品
     *
     * @param productInfoVO
     * @return
     */
    @RequestMapping(value = "/updateProduct")
    public ResultVO updateProduct(@RequestBody @Validated ProductInfoVO productInfoVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultVOUtil.error(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        String productId = productService.updateProduct(productInfoVO);
        return ResultVOUtil.success(productId);
    }

    /**
     * 查询商品
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "/selectProduct")
    public ResultVO selectProduct(String productId) {
        ProductInfoVO productInfo = productService.selectProduct(productId);
        return ResultVOUtil.success(productInfo);
    }

    /**
     * 查询商品
     *
     * @param page 第几页，从0开始
     * @param size 每页条数
     * @param name 查询名称
     * @return
     */
    @RequestMapping(value = "/listProduct")
    public ResultVO listProduct(Integer page, Integer size, Integer status, String name) {
        Page<ProductInfo> productInfoVOList = productService.listProduct(page, size, status, name);
        return ResultVOUtil.success(productInfoVOList);
    }


}
